package service;


import URL.Constants;

import bean.Block;
import com.alibaba.fastjson.JSONObject;
import requerst.HttpUtils;

import java.util.Base64;
import java.util.HashMap;
import java.util.Map;

public class GetBestBlockHash {
    /**
     * @author: dae
     * @email: 675116529@qq.com
     * @phone: 15270029507
     * @DateTime: 2021/12/16 10:46
     * 该类用于实现通过rpc访问统一bitcoin core 的getbestblockhash功能
     **/
    public String getBestBlockHash() {
        //准备rpc请求的用户身份信息
        String author = Constants.buser + ":" + Constants.bpwd;
        Base64.Encoder encoder = Base64.getEncoder();
        String authorBase64 = encoder.encodeToString(author.getBytes());
        Map<String, String> header = new HashMap<>();
        header.put("ContentType", "application/json");
        header.put("Authorization", "Basic " + authorBase64);
        //准备rpc请求的数据`
        Map<String, Object> body = new HashMap<>();
        body.put("id", 1001);
        body.put("method", "getbestblockhash");
        body.put("params", null);
        body.put("json_rpc", "2.0");
        byte[] bodyBytes = JSONObject.toJSONBytes(body);

        String result = HttpUtils.post(Constants.brpc_url, header, bodyBytes);
        Block block =JSONObject.parseObject(result, Block.class);
        return block.result;

    }


}

